
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

CreateConVar('swep_weldball_blastforce', 100)

function ENT:UnStatue( ent )
	if (!ent) then return end
	if (!ent:IsValid()) then return end
	if (ent:IsPlayer()) then return end	
	if (ent:GetClass() != "prop_ragdoll" ) then return false end
	if (ent:GetPhysicsObjectCount() < 2 ) then return false end
	if (ent:GetTable().StatueInfo == nil )  then return false end
	
	for key, val in pairs (ent:GetTable().StatueInfo.Welds) do
		if ( val && val:IsValid() ) then val:Remove() end
	end
	ent:GetTable().StatueInfo = nil
	local bones = ent:GetPhysicsObjectCount()
	for bone=1, bones do
		local bone1 = bone - 1
	end
end

function ENT:Initialize()
	self.Entity:SetModel( "models/weapons/w_gravball.mdl" )
	
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
    self.Entity:SetMoveType( MOVETYPE_VPHYSICS )   
    self.Entity:SetSolid( SOLID_VPHYSICS )
	
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
	end
	
	util.PrecacheSound( "weapons/physcannon/energy_sing_explosion2.wav" )
	util.PrecacheSound( "physics/metal/metal_grenade_impact_soft3.wav" )
	self.EntHealth = 50
	self.BlastForce = GetConVarNumber('swep_weldball_blastforce')
	self.HomingProps = GetConVarNumber('swep_weldball_homingprops')
end

function ENT:Explode()
	if !self or !self:IsValid() then return end
	local effectdata = EffectData()
		effectdata:SetEntity( self.Entity )
		effectdata:SetOrigin( self.Entity:GetPos() )
		effectdata:SetScale( 1 )
		effectdata:SetMagnitude( 1 )
		
	util.Effect( "cball_explode", effectdata )
	self.Entity:EmitSound( "weapons/physcannon/energy_sing_explosion2.wav",100, 80)
	
	constraint.RemoveConstraints( self.Entity, "Weld" )
	for _,contact in pairs(ents.FindInSphere(self.Entity:GetPos(), GetConVarNumber('swep_weldball_weldrange'))) do
		if contact:GetClass() ~= "npc_weldball_proj_dead" and !contact:IsPlayer() and !contact:IsNPC() and contact:GetPhysicsObject() and contact:GetPhysicsObject():IsValid() then
			for i=0,contact:GetPhysicsObjectCount()-1 do
				contact:GetPhysicsObjectNum(i):EnableMotion(true)
				contact:GetPhysicsObjectNum(i):EnableGravity(true)
				constraint.RemoveConstraints( contact, "Weld" )
			end
			self:UnStatue(contact)
			timer.Create("WeldballZapEffectUndo"..contact:EntIndex(),0.1,15,function()
				if !contact or !contact:IsValid() then return end
				local effectdata = EffectData()
					effectdata:SetEntity( contact )
					effectdata:SetStart( contact:GetPos() )
					effectdata:SetOrigin( contact:GetPos() )
					effectdata:SetAngle( contact:GetAngles() )
					effectdata:SetScale( 10 )
					effectdata:SetMagnitude( 10 )
				util.Effect( "TeslaHitBoxes", effectdata )
			end)
		end
	end
	
	timer.Simple( 0.01, self.LaunchProps, self )
end

function ENT:LaunchProps()
	if !self or !self:IsValid() then return end
	
	for _,contact in pairs(ents.FindInSphere(self.Entity:GetPos(), GetConVarNumber('swep_weldball_weldrange'))) do
		if contact:GetClass() ~= "npc_weldball_proj_dead" and !contact:IsPlayer() and !contact:IsNPC() and contact:GetPhysicsObject() and contact:GetPhysicsObject():IsValid() then
			local f = contact:GetPos() - self:GetPos()
			f:Normalize()
			f = f * 10000 * self.BlastForce
			contact:GetPhysicsObject():Wake()
			contact:GetPhysicsObject():ApplyForceCenter(f)
		end
	end
	
	self:Fire("kill",1,0.001)
end

function ENT:PhysicsCollide( data, physobj )
	if self:GetVelocity():Length() > 100 then self.Entity:EmitSound( "physics/metal/metal_grenade_impact_soft3.wav" ) end
end

function ENT:OnTakeDamage( dmginfo )
	self.Entity:TakePhysicsDamage( dmginfo )
	self.EntHealth = self.EntHealth-dmginfo:GetDamage()
	if self.EntHealth <= 0 then
		self:Explode()
	end
end
